/**
 * Created by caoRN on 2017/7/10.
 */
var LoginApp = angular.module("LoginApp", [
    "ui.bootstrap",
    "ngCookies",
    "ngResource",
    "toastr",
    "ngSanitize"

]);
LoginApp.factory('settings', ['$rootScope', function ($rootScope) {
    // supported languages
    var settings = {
        assetsPath: './assets'
    };

    $rootScope.settings = settings;
    return settings;
}]);
//通知消息全局配置
angular.module("LoginApp").config(function (toastrConfig) {
    angular.extend(toastrConfig, {
        autoDismiss: false,
        containerId: 'toast-container',
        maxOpened: 0,
        newestOnTop: true,
        positionClass: 'toast-bottom-right',
        preventDuplicates: false,
        preventOpenDuplicates: false,
        target: 'body',
        closeButton: true
    });
});
angular.module("LoginApp").controller('LoginController',
    ['$scope', '$rootScope', '$window', '$cookieStore', '$http', '$location', '$resource', '$cookies','toastr',
        function ($scope, $rootScope, $window, $cookieStore, $http, $location, $resource, $cookies,toastr) {
            $(document).ready(function () {
                var $login = $(".login");
                $login.backstretch([
                        $rootScope.settings.assetsPath + "/pages/media/bg/1.jpg",
                        $rootScope.settings.assetsPath + "/pages/media/bg/2.jpg",
                        $rootScope.settings.assetsPath + "/pages/media/bg/3.jpg",
                        $rootScope.settings.assetsPath + "/pages/media/bg/4.jpg",
                        $rootScope.settings.assetsPath + "/pages/media/bg/5.jpg",
                        $rootScope.settings.assetsPath + "/pages/media/bg/6.jpg"
                    ], {
                        fade: 1000,
                        duration: 8000
                    }
                );
            });
            var loginUrl = "/platform/login";

            var remember = $.cookie("remeberMe");
            if (remember && remember == "true") {
                remember = true;
            } else {
                remember = false;
            }
            $scope.remeberMe = remember || false;
            $scope.username = "";
            if ($scope.remeberMe) {
                $scope.username = $.cookie("username") || "";
            }
            // 登陆错误
            $scope.loginError = false;
            $scope.loginMsg = '';
            // 登陆点击标志
            $scope.LoginFlag = false;
            $scope.login = function (formInvalid) {
                $scope.LoginFlag = true;
                if (formInvalid) return;
                var param = {
                    loginId: $scope.username,
                    password: $scope.password
                };

                $resource(loginUrl, null, {'login': {method: 'POST'}}).login(param).$promise.then(function (result) {
                    if (result.status === 'success') {
                        $cookies.put("nickname", $scope.username);
                        $.cookie("remeberMe", $scope.remeberMe);
                        if ($scope.remeberMe) {
                            $.cookie("username", $scope.username);
                        }
                        $window.location.href = "./index.html#/dashboard.html";//跳转页面
                    } else {
                        for (var i = 0; i < result.errors.length; i++) {
                            toastr.error(result.errors[i].errmsg, "登录失败");
                        }
                    }
                });
            };


            //回车事件
            document.onkeydown = keydown;
            function keydown(e) {
                var currKey = 0, e = e || event;
                currKey = e.keyCode || e.which || e.charCode;//支持IE、FF
                if (currKey == 13) {
                    $scope.login($scope.loginForm.$invalid);
                }
            }

        }]);

LoginApp.run(["$rootScope", "settings", function ($rootScope, settings) {
    $rootScope.$settings = settings;
}]);
